<template>
  <div class="mod-activiti__process">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.processName" placeholder="流程名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="流程标识" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="deployHandle()">部署流程文件</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="流程ID">
      </el-table-column>
      <el-table-column
        prop="deploymentId"
        header-align="center"
        align="center"
        width="80"
        label="部署ID">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="流程名称">
      </el-table-column>
      <el-table-column
        prop="key"
        header-align="center"
        align="center"
        label="流程标识">
      </el-table-column>
      <el-table-column
        prop="version"
        header-align="center"
        align="center"
        label="流程版本">
      </el-table-column>
      <el-table-column
        prop="resourceName"
        header-align="center"
        align="center"
        width="100"
        :show-overflow-tooltip="true"
        label="流程XML">
        <template slot-scope="scope">
          <a :href="getResourceURL(scope.row.deploymentId, scope.row.resourceName)" target="_blank">{{ scope.row.resourceName }}</a>
        </template>
      </el-table-column>
      <el-table-column
        prop="diagramResourceName"
        header-align="center"
        align="center"
        width="100"
        :show-overflow-tooltip="true"
        label="流程图片">
        <template slot-scope="scope">
          <a :href="getResourceURL(scope.row.deploymentId, scope.row.diagramResourceName)" target="_blank">{{ scope.row.diagramResourceName }}</a>
        </template>
      </el-table-column>
      <el-table-column
        prop="deploymentTime"
        header-align="center"
        align="center"
        width="180"
        label="部署时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.suspended" type="text" size="small" @click="activeHandle(scope.row.id)">激活</el-button>
          <el-button v-else type="text" size="small" @click="suspendHandle(scope.row.id)">挂起</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.deploymentId)">删除</el-button>
          <el-button type="text" size="small" @click="convertToModelHandle(scope.row.id)">转换为模型</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 部署流程文件 -->
    <deploy v-if="deployVisible" ref="deploy" @refreshDataList="getDataList"></deploy>
  </div>
</template>

<script>
  import Deploy from './process-deploy'
  import Cookies from 'js-cookie'
  import qs from 'qs'
  export default {
    data () {
      return {
        dataForm: {
          processName: '',
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        deployVisible: false
      }
    },
    components: {
      Deploy
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http.get(
          `${window.SITE_CONFIG['baseURL']}/act/process/list`,
          {
            params: {
              'page': this.pageIndex,
              'limit': this.pageSize,
              'processName': this.dataForm.processName,
              'key': this.dataForm.key
            }
          }
        ).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data
            this.totalPage = data.count
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 获取流程(xml/image)url地址
      getResourceURL (id, name) {
        var params = {
          'token': Cookies.get('token'),
          'deploymentId': id,
          'resourceName': name
        }
        return `${window.SITE_CONFIG['baseURL']}/act/process/resource?${qs.stringify(params)}`
      },
      // 部署流程文件
      deployHandle () {
        this.deployVisible = true
        this.$nextTick(() => {
          this.$refs.deploy.init()
        })
      },
      // 激活
      activeHandle (id) {
        this.$confirm(`确定对[流程id=${id}]进行[激活]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.post(
            `${window.SITE_CONFIG['baseURL']}/act/process/active`,
            {
              'id': id
            },
            {
              headers: { 'content-type': 'application/x-www-form-urlencoded' }
            }
          ).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      // 挂起
      suspendHandle (id) {
        this.$confirm(`确定对[流程id=${id}]进行[挂起]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.post(
            `${window.SITE_CONFIG['baseURL']}/act/process/suspend`,
            {
              'id': id
            },
            {
              headers: { 'content-type': 'application/x-www-form-urlencoded' }
            }
          ).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.deploymentId
        })
        this.$confirm(`确定对[部署id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.post(
            `${window.SITE_CONFIG['baseURL']}/act/process/delete`,
            ids
          ).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      // 转换为模型
      convertToModelHandle (id) {
        this.$confirm(`确定对[流程id=${id}]进行[转换为模型]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.post(
            `${window.SITE_CONFIG['baseURL']}/act/process/convertToModel`,
            {
              'id': id
            },
            {
              headers: { 'content-type': 'application/x-www-form-urlencoded' }
            }
          ).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
